﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using TestEngine;

namespace TestWindowsGame1
{
    class inGame3DScene : Scene
    {
        Actor3D _actor;

        List<Keys> InputKeys = new List<Keys>();

        Keys[][] Combos = {new Keys[]{Keys.Right, Keys.Right},
                           new Keys[]{Keys.Left, Keys.Right, Keys.B},
                           new Keys[]{Keys.Down, Keys.Right, Keys.A},
                           new Keys[]{Keys.Left, Keys.Down, Keys.Right, Keys.A}};

        InputManager _inputManager;
        SpriteFont _debugFont;
        Utility _utility;
        //List<Actor3D> _actors3D;

        Camera _cam;

        public inGame3DScene(ContentManager content, GraphicsDeviceManager Graphics)
        {
            Content = content;
            _graphics = Graphics;
            _debugFont = Content.Load<SpriteFont>("SpriteFont1");

            _actor = new Actor3D();
            _actor.model = Content.Load<Model>("Models/Crate_Fragile"); //Currently loading BMPs from an outside Folder(Should Delete when Model is Discarted)

            //_actors3D = new List<Actor3D>();
            //--TestingSubjects--

            //_actors3D.Add(_actor);//TS01

            _inputManager = new InputManager(InputKeys, Combos);
            _utility = new Utility(_graphics);
            _cam = new Camera();
            _cam.targetToFollow = _actor;

            InputKeys.Add(Keys.Left);
            InputKeys.Add(Keys.Right);
            InputKeys.Add(Keys.Up);
            InputKeys.Add(Keys.Down);
            InputKeys.Add(Keys.A);
            InputKeys.Add(Keys.B);
        }
        public override void update(int d_time)
        {
            _inputManager.update(d_time);
            _cam.update(d_time);
            base.update(d_time);
        }
        public override void draw(SpriteBatch sb)
        {
            Matrix[] transforms = new Matrix[_actor.model.Bones.Count];
            _actor.model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in _actor.model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(_actor.rotation.Y) * Matrix.CreateTranslation(_actor.position);
                    effect.View = _cam.View;
                    effect.Projection = _cam.Proj;
                    //Something, something... dark side of the force
                }
                mesh.Draw();
            }
            base.draw(sb);
        }
        public override void hnd_input()
        {
            if (_inputManager.isNewKeyPress(Keys.C))
            {
                if (!_cam.isThirdPerson)
                {
                    _cam.isThirdPerson = true;
                }
                else
                {
                    _cam.isThirdPerson = false;
                }
            }
            if (!_cam.isThirdPerson)
            {
                if (_inputManager.isKeyPressed(Keys.Left))
                {
                    _cam.RotateCam(0, 0.05f, 0);
                }
                if (_inputManager.isKeyPressed(Keys.Right))
                {
                    _cam.RotateCam(0, -0.05f, 0);
                }
                if (_inputManager.isKeyPressed(Keys.Up))
                {
                    _cam.position += _cam.forward;
                }
                if (_inputManager.isKeyPressed(Keys.Down))
                {
                    _cam.position -= _cam.forward;
                }
                if (_inputManager.isKeyPressed(Keys.Z))
                {
                    _cam.position -= _cam.left;
                }
                if (_inputManager.isKeyPressed(Keys.X))
                {
                    _cam.position += _cam.left;
                }
            }
            base.hnd_input();
        }
    }
}
